/* GO THRU ITEM PLANNING DETAIL TABLE TO FIND THE PART */
/* AND THEN CHECK FOR ITS BOM                          */
procedure p_find_ptp_det:

   define input  parameter ip-check-level   like mfc_logical no-undo.
   define input  parameter ip-find-psmstr   like mfc_logical no-undo.

   define variable l_ptp_avail like mfc_logical no-undo.

   l_ptp_avail = no.

   if ip-find-psmstr
      then
   find first ps_mstr
      where  recid(ps_mstr) =  -1
   no-lock no-error.


   for each ptp_det
         fields (ptp_domain ptp_bom_code ptp_part)
         where ptp_domain   = global_domain
         and   ptp_bom_code = comp
      no-lock:

      l_ptp_avail = yes.

      if ip-find-psmstr
      then do:

         find first ps_mstr
            use-index ps_comp
            where ps_domain   = global_domain
            and   ps_comp     = ptp_det.ptp_part
            and  ((ps_start  <= eff_date and
                   ps_end     = ?)
               or (ps_start   = ?        and
                   ps_end    >= eff_date)
               or (ps_start   = ?        and
                   ps_end     = ?)
               or (ps_start  <= eff_date and
                   ps_end    >= eff_date))
         no-lock no-error.

         if not available ps_mstr
            then
            next.

      end. /* IF ip-find-psmstr */

      run p_check_item_bom(input  ptp_det.ptp_part,
         input  ptp_det.ptp_bom_code,
         input  ps_mstr.ps_par,
         input  ps_mstr.ps_ref,
         input  ip-check-level,
         output l_leave).


      if l_leave
      then do:

         l_leave = false.
         leave.

      end. /* IF l_leave */

   end. /* FOR EACH ptp_det */

   if not l_ptp_avail
   then do:
      for each pt_mstr
            fields(pt_domain pt_part pt_bom_code)
            where pt_domain   = global_domain
            and   pt_bom_code = comp
         no-lock:

         if ip-find-psmstr
         then do:
            for first ps_mstr
               use-index ps_comp
               where ps_domain   = global_domain
               and   ps_comp     = pt_part
               and  ((ps_start  <= eff_date and
                      ps_end     = ?)
                  or (ps_start   = ?        and
                      ps_end    >= eff_date)
                  or (ps_start   = ?        and
                      ps_end     = ?)
                  or (ps_start  <= eff_date and
                      ps_end    >= eff_date))
            no-lock:
            end. /* FOR FIRST ps_mstr */

            if not available ps_mstr
               then
               next.

         end. /* IF ip-find-psmstr */

         run p_check_item_bom(input  pt_part,
            input  pt_bom_code,
            input  ps_mstr.ps_par,
            input  ps_mstr.ps_ref,
            input  ip-check-level,
            output l_leave).
         if l_leave
         then do:
            l_leave = false.
            leave.
         end. /* IF l_leave */
      end. /* FOR EACH pt_mstr */
   end. /* IF NOT l_ptp_avail */
end procedure. /* PROCEDURE p_find_ptp_det */








/* CHECK IF THE GIVEN COMBINATION OF PART, BOM CODE, PARENT AND  */
/* REFERENCE NUMBER IS ALREADY EXISTING. IF NOT EXISTING THEN WE */
/* CREATE THE TEMP TABLE OTHERWISE WE CLEAR THE ps_mstr BUFFER   */
procedure p_check_item_bom:

   define input  parameter ip-item          like ptp_part     no-undo.
   define input  parameter ip-bom           like ptp_bom_code no-undo.
   define input  parameter ip-par           like ps_par       no-undo.
   define input  parameter ip-ref           like ps_ref       no-undo.
   define input  parameter ip-check-level   like mfc_logical  no-undo.
   define output parameter op-leave         like mfc_logical  no-undo.

   for first item-bom
      where  item-bom.item = ip-item
      and    item-bom.bom  = ip-bom
      and    item-bom.par  = ip-par
      and    item-bom.ref  = ip-ref
   no-lock:
   end. /* FOR FIRST item-bom */

   if not available item-bom
   then do:

      run p_create_item_bom(input ip-item,
         input ip-bom,
         input ip-par,
         input ip-ref).

      op-leave = true.

   end.  /* IF NOT AVAILABLE item-bom */
   else do:
      if  (ip-check-level
         and level >= maxlevel
         and maxlevel <> 0)
         or not ip-check-level
      then do:

         find first ps_mstr
            where  recid(ps_mstr) =  -1
         no-lock no-error.

         op-leave = true.

      end. /* IF  (ip-check-level... */

   end. /* ELSE DO */

end procedure. /* PROCEDURE p_check_item_bom */


/* CREATE TEMP-TABLE WITH A PART,BOM CODE,PARENT */
/* AND REFERENCE NUMBER COMBINATION              */
procedure p_create_item_bom:

   define input parameter ip-item like pt_part     no-undo.
   define input parameter ip-bom  like pt_bom_code no-undo.
   define input parameter ip-par  like ps_par      no-undo.
   define input parameter ip-ref  like ps_ref      no-undo.

   create item-bom.
   assign
      item-bom.item = ip-item
      item-bom.bom  = ip-bom
      item-bom.par  = ip-par
      item-bom.ref  = ip-ref.

end procedure. /* PROCEDURE p_create_item_bom */
